<template>
  <div class="add">
    <el-dialog title="新增员工" :visible="showdialog" @close="cancel">
      <el-form ref="myform" label-width="100px" :model="FormData" :rules="rules">
        <el-form-item label="姓名:" prop="username">
          <el-input v-model="FormData.username" placeholder="1-30个字符" />
        </el-form-item>
        <el-form-item label="手机:" prop="mobile">
          <el-input v-model="FormData.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="入职时间:" prop="timeOfEntry">
          <el-date-picker
            v-model="FormData.timeOfEntry"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="聘用形式:" prop="formOfEmployment">
          <el-select v-model="FormData.formOfEmployment" placeholder="请选择">
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工号:" prop="workNumber">
          <el-input v-model="FormData.workNumber" placeholder="1-20个字符" />
        </el-form-item>
        <el-form-item label="部门:" prop="departmentName" class="tree">
          <el-input
            v-model="FormData.departmentName"
            placeholder="请选择"
            @focus="myfocus"
          />
          <!-- 部门树状结构 -->
          <el-tree
            v-if="showTree"
            :data="deptsList"
            :props="{label:'name'}"
            default-expand-all

            :expand-on-click-node="false"
            @node-click="nodeclick"
          />
        </el-form-item>
        <el-form-item label="转正时间:" prop="correctionTime">
          <el-date-picker
            v-model="FormData.correctionTime"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//
import myemployees from '@/api/constant/employees'
// 导入部门结构接口
import { companyDepartmentApi } from '@/api/departments'
// 导入新增员工接口
import { addUser } from '@/api/employees'
// 导入手机验证规则
import { validateMobile } from '@/utils/validate'
export default {
  props: ['showdialog'],
  data() {
    return {
      // 下拉框中的数据源
      typeList: myemployees.hireType,
      showTree: false,
      deptsList: [],
      FormData: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      },
      rules: {
        username: [{ required: true, message: '姓名不能为空', trigger: 'blur' },
          { min: 1, max: 30, message: '只能输入1到30字符', trigger: 'blur' }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          validator: validateMobile
        }],
        timeOfEntry: [{ required: true, message: '入职时间不能为空', trigger: 'blur' }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' },
          { min: 1, max: 20, message: '只能输入1到20字符', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        correctionTime: [{ required: true, message: '转正时间不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getdeptList()
  },
  methods: {
    // 获取部门结构
    async getdeptList() {
      const res = await companyDepartmentApi()
      console.log(res)
      this.deptsList = this.turnData(res.depts, '')
    },
    // 取消事件
    cancel() {
      this.$emit('update:showdialog', false)
    },
    // 确定提交
    submit() {
      this.$refs.myform.validate(async isOK => {
        const res = await addUser(this.FormData)
        console.log(res)
        this.FormData = {
          username: '',
          mobile: '',
          timeOfEntry: '',
          formOfEmployment: '',
          workNumber: '',
          departmentName: '',
          correctionTime: ''
        }
        this.$message.success('新增成功')
        this.$emit('update:showdialog', false)
        this.$refs.myform.resetFields()
        this.$emit('getemplyeesInfo')
      })
    },
    // 部门节点被点击时的回调
    nodeclick(data) {
      console.log(name)
      this.FormData.departmentName = data.name
      this.showTree = false
    },
    // 输入框获取焦点的方法
    myfocus() {
      this.showTree = true
    },
    turnData(arr, pid) {
      const newArr = []
      arr.forEach(item => {
        if (item.pid === pid) {
          const children = this.turnData(arr, item.id)
          if (children.length > 0) {
            item.children = children
          }
          newArr.push(item)
        }
      })
      return newArr
    }
  }
}
</script>

<style lang="scss">
.add{
    .el-dialog{
      width: 50%;
    overflow: hidden;
    .el-form{
        width: 400px;
    }}
    .tree{
      width: 400px;
      position: relative;
      .el-tree{
        position: absolute;
        left: 0;
        z-index: 999;
        width: 100%;
        border: 1px solid #ccc;
        border-radius: 6px;
      }
    }
}
</style>
